<script>
import CommonOperationForm from "@/components/CommonOperationForm/index.vue";
import {getTemplateList} from "@/api/template";
import {getUserInfo} from "@/utils/auth";

export default {
  props: ["printDialogVisible", "typeName"],
  components: {
    CommonOperationForm
  },
  watch: {
    printDialogVisible() {
      if (this.printDialogVisible) {
        this.print();
      }
    },
  },
  data() {
    return {
      printDialog: {
        templateData: {
          templateTypeName: '',
          templateID: '',
          template: {},
        },
        printDialogVisible: false,
        list: [
          {
            text: '选择模板：',
            required: true,
            selectWidth: '280px',
            width: '80px',
            field: 'documentType',
            type: 'selectAllowCreate',
            allowCreate: false,
            options: [],
            value: '',
            changeEvent: (v) => {
              console.log(v);
            }
          },
          {
            value: '',
            width: '85px',
            rowWidth: '100%',
            field: 'defaultSupplier',
            type: 'addButton',
            clickEvent: (v) => {
              if (v === 'add') {

              } else {
                this.printDialog.printDialogVisible = true;
                this.printDialog.templateData.template = this.printDialog.list[0].value.template;
                this.printDialog.templateData.templateID = this.printDialog.list[0].value._id;
                this.printDialog.templateData.templateTypeName = this.printDialog.list[0].value.businessType;
                this.$emit('setPrintData', this.printDialog);
              }
            }
          },
        ],
        dialogLoading: false,
        templateDialogVisible: false,
      },
    }
  },
  methods: {
    print() {
      this.printDialog.templateDialogVisible = true;
      this.printDialog.dialogLoading = true;
      // get
      getTemplateList({
        userID: getUserInfo().userID,
        businessType: this.typeName.replace("单", ""),
      }).then(res => {
        console.log(res);
        this.printDialog.dialogLoading = false;
        let {data} = res.data;
        this.printDialog.list[0].options = [];
        for (let i = 0; i < data.length; i++) {
          this.printDialog.list[0].options.push({
            label: data[i].templateName,
            value: data[i],
          });

          if (data[i].defaultType) {
            this.printDialog.list[0].value = data[i];
          }
        }
      });
    },
    handleClose() {
      this.printDialog.templateDialogVisible = false;
      this.$emit('setPrintData', {
        templateDialogVisible: false,
      });
    },
    templateClick(v) {
      if (v === '2') {

      }

    },
  }
}
</script>

<template>
  <el-dialog style="margin-top: 10px;" custom-class="dialoginter101"
             :close-on-click-modal="false" :modal="false"
             width="40%" v-dialog-drag   title="打印" @close="handleClose"
             :visible="printDialog.templateDialogVisible">
    <common-operation-form v-loading="printDialog.dialogLoading" style="margin-top: 6px;"  :margin-top="'0px'" :list="printDialog.list"/>
    <span slot="footer" class="dialog-footer">
          <el-button size="small" @click="templateClick('1')">刷新</el-button>
          <el-button type="primary" size="small" @click="templateClick('2')">管理模板</el-button>
          <el-button type="primary" size="small" @click="templateClick('3')">分享为图片</el-button>
          <el-button type="primary" size="small" @click="templateClick('4')">下载PDF</el-button>
      </span>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
